package com.dgut.doctorservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dgut.commonutils.Result;
import com.dgut.doctorservice.entity.MedecineInfo;
import com.dgut.doctorservice.entity.Prescription;
import com.dgut.doctorservice.service.MedecineInfoService;
import com.dgut.doctorservice.service.PrescriptionService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.*;
/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dgut
 * @since 2021-09-18
 */
@RestController
@RequestMapping("/doctorservice/prescription")
@CrossOrigin
public class PrescriptionController {

    @Autowired
    PrescriptionService service;

    @Autowired
    MedecineInfoService medecineInfoService;


    @ApiOperation("根据病人id获取所有取药日期")
    @PostMapping("getAllDate")
    public Result getAllDate(@RequestParam String id){
        List<String> dates=new ArrayList<>();
        QueryWrapper<Prescription> wrapper = new QueryWrapper<>();
        wrapper.eq("p_id",id);
        //wrapper.eq("deleted",1);
        List<Prescription> list = service.list(wrapper);
        for (int i = 0; i <list.size() ; i++) {
            dates.add(list.get(i).getDate());
        }
        HashSet hashSet = new HashSet(dates);
        dates.clear();
        dates.addAll(hashSet);
        return Result.succ().data("dates",dates);
    }

    @ApiOperation("病人进行药品缴费")
    @PostMapping("payment")
    public Result payment(@RequestParam String date,@RequestParam String id) {
        double total=0;
        QueryWrapper<Prescription> wrapper = new QueryWrapper<>();
        wrapper.eq("p_id", id);
        wrapper.eq("date", date);
        List<Prescription> list = service.list(wrapper);
        if (list == null) {
            return Result.succ().message("暂时没有药单");
        } else {
            Map<String, Object> map = new HashMap<>();
            List<MedecineInfo> lists = new ArrayList<>();
            List list1 = new ArrayList();
            for (int i = 0; i < list.size(); i++) {
                QueryWrapper<MedecineInfo> wrapper1 = new QueryWrapper<>();
                wrapper1.eq("id", list.get(i).getMId());
                MedecineInfo one = medecineInfoService.getOne(wrapper1);
                lists.add(one);
                list.get(i).setFlags(1);
                service.updateById(list.get(i));
                total+=list.get(i).getNumber()*one.getSell();
                list.get(i).setFlags(1);
                service.updateById(list.get(i));
                //one.setNumber(one.getNumber()-list.get(i).getNumber());
                //QueryWrapper<Prescription> wrapper2=new QueryWrapper<>();
                //wrapper2.eq("m_id",list.get(i).getMId());
                //service.remove(wrapper2);
                //medecineInfoService.updateById(one);
            }
            map.put("medecineInfo", lists);
            map.put("number", list);
            return Result.succ().data(map).data("total",total);
        }
    }

    @ApiOperation("根据日期获取病人取药记录")
    @PostMapping("getMedecineByDate")
    public Result getMedecineByDate(@RequestParam String date,@RequestParam String id){
        QueryWrapper<Prescription> wrapper=new QueryWrapper<>();
        wrapper.eq("p_id",id);
        wrapper.eq("date",date);
        List<Prescription> list = service.list(wrapper);
        if (list==null){
            return Result.succ().message("暂时没有药单");
        }else {
            Map<String, Object> map = new HashMap<>();
            List<MedecineInfo> lists = new ArrayList<>();
            List list1 = new ArrayList();
            for (int i = 0; i < list.size(); i++) {
                QueryWrapper<MedecineInfo> wrapper1 = new QueryWrapper<>();
                wrapper1.eq("id", list.get(i).getMId());
                MedecineInfo one = medecineInfoService.getOne(wrapper1);
                lists.add(one);
                //one.setNumber(one.getNumber()-list.get(i).getNumber());
                //QueryWrapper<Prescription> wrapper2=new QueryWrapper<>();
                //wrapper2.eq("m_id",list.get(i).getMId());
                //service.remove(wrapper2);
                //medecineInfoService.updateById(one);
            }
            map.put("medecineInfo", lists);
            map.put("number", list);
            return Result.succ().data(map);
        }
    }


    @ApiOperation("前端进行药品选择，然后以列表的形式传过来，从而实现医生开药")
    @PostMapping("rescribeMedicine")
    public Result rescribeMedicine(@RequestBody List<Prescription> lists){
        for (int i = 0; i <lists.size() ; i++) {
            service.save(lists.get(i));
        }
        return Result.succ();
    }


    @ApiOperation("病人到前台通过药单进行取药")
    @GetMapping("getMedecines")
    public Result getMedecines(@RequestParam String id,@RequestParam String date){
        QueryWrapper<Prescription> wrapper=new QueryWrapper<>();
        wrapper.eq("p_id",id);
        wrapper.eq("date",date);
        List<Prescription> list = service.list(wrapper);
        if (list==null){
            return Result.succ().message("暂时没有药单");
        }else{
            for (int i = 0; i <list.size() ; i++) {
                QueryWrapper<MedecineInfo> wrapper1=new QueryWrapper<>();
                wrapper1.eq("id",list.get(i).getMId());
                MedecineInfo one = medecineInfoService.getOne(wrapper1);
                one.setNumber(one.getNumber()-list.get(i).getNumber());
                QueryWrapper<Prescription> wrapper2=new QueryWrapper<>();
                wrapper2.eq("m_id",list.get(i).getMId());
                service.remove(wrapper2);
                medecineInfoService.updateById(one);
            }
            return Result.succ();
        }
    }

    @ApiOperation("根据病人的id获取药单")
    @PostMapping("getMedecineOrder/{id}")
    public Result getMedecineOrder(@RequestParam String id){
        QueryWrapper<Prescription> wrapper=new QueryWrapper<>();
        wrapper.eq("p_id",id);
        List<Prescription> list = service.list(wrapper);
        if (list==null){
            return Result.succ().message("暂时没有药单");
        }else{
            Map<String,Object>map=new HashMap<>();
            List<MedecineInfo> lists=new ArrayList<>();
            List list1=new ArrayList();
            for (int i = 0; i <list.size() ; i++) {
                QueryWrapper<MedecineInfo> wrapper1=new QueryWrapper<>();
                wrapper1.eq("id",list.get(i).getMId());
                MedecineInfo one = medecineInfoService.getOne(wrapper1);
                lists.add(one);
                //one.setNumber(one.getNumber()-list.get(i).getNumber());
                //QueryWrapper<Prescription> wrapper2=new QueryWrapper<>();
                //wrapper2.eq("m_id",list.get(i).getMId());
                //service.remove(wrapper2);
                //medecineInfoService.updateById(one);
            }
            map.put("medecineInfo",lists);
            map.put("number",list);
            return Result.succ().data(map);
        }
    }



}

